package com.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.entity.ChanPing;
import com.entity.YongHuHangYe;
import com.service.CpService;
import com.service.UserIndustryService;




@Controller
@RequestMapping("/cp/manager")
public class CpController{
	
	@Autowired
	private CpService cpService;
	@Autowired
	private UserIndustryService uiService;
	
	
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(ChanPing cp,Page<ChanPing> page){
		AjaxRes res = new AjaxRes();
		System.out.println(cp.getId());
		page = cpService.findByPage(cp, page);
		res.setSucceed(page,"成功");
		return res;
	}
	@RequestMapping("/find")
	@ResponseBody
	public AjaxRes find(ChanPing cp){
		AjaxRes res = new AjaxRes();
		System.out.println(cp.getId());
		List<ChanPing> cplist=cpService.find(cp);
		res.setSucceed(cplist,"成功");
		return res;
	}
	
	@RequestMapping("/addcp")
	@ResponseBody
	public AjaxRes addcp(ChanPing cp,String fujianId){
		AjaxRes res = new AjaxRes();
		try {
			YongHuHangYe yhhy=new YongHuHangYe();
			yhhy.setHymc(cp.getCpfl());
			System.out.println("---------"+yhhy);
			yhhy=uiService.findByid(yhhy);
			System.out.println("_________________"+yhhy);
			  Date dt = new Date(); 
			 String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
			cp.setCpfbsj(dateStr);
			cp.setShsj(dateStr);
			cp.setCpfl(yhhy.getId());
			cpService.insertFj(cp,fujianId);
			res.setResMsg("添加成功");
		} catch (Exception e) {
			
			res.setResMsg("添加失败");
		}
		return res;	
		
	}
	@RequestMapping("/updatecp")
	@ResponseBody
	public AjaxRes updatecp(ChanPing cp){
		AjaxRes res = new AjaxRes();
		YongHuHangYe yhhy=new YongHuHangYe();
		yhhy.setHymc(cp.getCpfl());
		System.out.println("---------"+yhhy);
		yhhy=uiService.findByid(yhhy);
		System.out.println("_________________"+yhhy);
		cp.setCpfl(yhhy.getId());
		cpService.update(cp);
		res.setSucceedMsg("修改成功");
		return res;
	}
	@RequestMapping("/upcpzt")
	@ResponseBody
	public AjaxRes upcpzt(ChanPing cp){
		AjaxRes res = new AjaxRes();
		System.out.println(cp.getCpzt());
		cpService.upcpzt(cp);
		res.setSucceedMsg("修改成功");
		return res;
	}
	
	@RequestMapping("/delAllcp")
	@ResponseBody
	public AjaxRes delAllcp(ChanPing cp){
		AjaxRes res = new AjaxRes();
		cpService.deletes(cp);
		res.setSucceedMsg("删除审核未通过的产品成功");
		return res;
	}
	@RequestMapping("/delcp")
	@ResponseBody
	public AjaxRes delcp(ChanPing cp){
		AjaxRes res = new AjaxRes();
		cpService.delete(cp);
		res.setSucceedMsg("删除成功");
		return res;
	}

	

}
